//
// Created by none98 on 2021/9/24.
//

#ifndef LOGINSERVER_DATABASECONNPOOL_H
#define LOGINSERVER_DATABASECONNPOOL_H

#include <vector>
#include <mutex>
#include <condition_variable>
#include "mysql/mysql/jdbc.h"

class DatabaseConnPool{
public:
    DatabaseConnPool():
        driver(nullptr)
    {
    }

    ~DatabaseConnPool()=default;

    void InitPool(const std::string &hostname, const std::string username, const std::string &password);
    void CloseConnection(sql::Connection* conn);

    sql::Connection* GetConnection();


private:
    std::list<sql::Connection*> connections;
    std::vector<std::unique_ptr<sql::Connection>> realConnections;
    std::mutex connsMutex;
    std::condition_variable connsVar;
    sql::mysql::MySQL_Driver* driver;
};


#endif //LOGINSERVER_DATABASECONNPOOL_H
